package com.wpre.blogsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.wpre.blogsystem.common.pojo.dataobject.BlogInfo;
import com.wpre.blogsystem.common.pojo.request.AddBlogParam;
import com.wpre.blogsystem.common.pojo.request.UpdateBlogParam;
import com.wpre.blogsystem.common.pojo.response.BlogInfoResponse;
import com.wpre.blogsystem.common.pojo.response.BlogInfoResponseSimple;
import com.wpre.blogsystem.common.utils.BeanTransfer;
import com.wpre.blogsystem.exception.BlogException;
import com.wpre.blogsystem.mapper.BlogInfoMapper;
import com.wpre.blogsystem.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-04
 * Time: 14:42
 */
@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    //获取博客列表
    @Override
    public List<BlogInfoResponseSimple> getList() {
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(new LambdaQueryWrapper<BlogInfo>().eq(BlogInfo::getDeleteFlag, 0)
                .orderByDesc(BlogInfo::getId));
        List<BlogInfoResponseSimple> blogInfoResponses = blogInfos.stream().map(blogInfo -> {
            BlogInfoResponseSimple blogInfoResponse = new BlogInfoResponseSimple();
            BeanUtils.copyProperties(blogInfo, blogInfoResponse);
            return blogInfoResponse;
        }).collect(Collectors.toList());
        return blogInfoResponses;
    }

    //获取博客详情
    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = selectBlogInfoById(blogId);
        return BeanTransfer.trans(blogInfo);//转换
    }

    @Override
    public boolean addBlog(AddBlogParam param) {
        //对象转换,AddBlogParam-->BlogInfo
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(param, blogInfo);
        //插入数据库
        int insert = 0;
        try {
            insert = blogInfoMapper.insert(blogInfo);
        } catch (BlogException e) {
            log.info("插入失败{ }", e);
            return false;
        }
        return insert == 1;
    }

    @Override
    public boolean updateBlog(UpdateBlogParam param) {
        //对象转换UpdateBlogParam->BlogInfo
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(param, blogInfo);
        //更新
        int result = 0;
        try {
            result = blogInfoMapper.updateById(blogInfo);
            if (result == 1) {
                return true;
            }
        } catch (Exception e) {
            log.error("更新失败{ }", e);
            return false;
        }
        return false;
    }

    //删除博客
    @Override
    public boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        int ret = 0;
        try {
            ret = blogInfoMapper.updateById(blogInfo);
            if (ret == 1) {
                return true;
            }
        } catch (Exception e) {
            log.error("删除失败", e);
            return false;
        }
        return false;
    }

    //通过BlogId查询
    public BlogInfo selectBlogInfoById(Integer blogId) {
        return blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo::getDeleteFlag, 0));
    }

}
